#!/bin/bash
#彩蛋：lick-run脚本基本全都被和谐了~
#搞这么复杂其实是为了原来的文案。。。当然，又不是不能用.jpeg
#
#
#
if [ "$1" = "" ];then
echo "错误：没有指定监测路径"
exit 0
fi

the_licker=`eval find $1 -name *.lick | head -n 1`

if [ "$the_licker" != "" ];then
        filepath="`echo ${the_licker%/*} `"
        #获取licker所在目录
        echo
        echo 
        touch "$filepath/水水.txt"
        #准备收集echo的文案
        echo ---------------------------------------------------------
        echo "检测到.lick文件存在。准备执行脚本，会在执行结束后把日志发到这里"
        echo "检测的时间是:"
        echo "以下是执行日志。日志结束后可选附上ninan文案以帮助查错。如果需要把log输出，请取消注释。即使不取消注释，运行结果也会在.lick所在目录输出。"
        date
        echo "检测到.lick文件存在。准备执行脚本" >> "$filepath/ninan.txt"
        echo "脚本位置在" >> "$filepath/ninan.txt"
        echo "$the_licker" >> "$filepath/ninan.txt"
        #回显 licker 的位置
        
        
        echo "脚本所在路径为" >> "$filepath/ninan.txt"
        echo "$filepath"  >> "$filepath/ninan.txt"
        # 回显filepath
        echo "收到的指令如下：" >> "$filepath/ninan.txt"
        
        
        echo "" >> "$filepath/ninan.txt"
        cat "$the_licker"  >> "$filepath/ninan.txt"
        echo "" >> "$filepath/ninan.txt"
        
        
        
        echo "将脚本移动到~/mox-daemons/keep-slimy/lick-run/momo-tmp/"  >> "$filepath/ninan.txt"
        cp "$the_licker" ~/mox-daemons/keep-slimy/lick-run/momo-tmp/run.sh 
        mv "$the_licker" "$the_licker""ed"
        echo "cp $the_licker ~/mox-daemons/keep-slimy/lick-run/momo-tmp/run.sh " >> "$filepath/ninan.txt"
        
        
        echo "赋予可执行权限" >> "$filepath/ninan.txt"
        chmod +x ~/mox-daemons/keep-slimy/lick-run/momo-tmp/run.sh 
        echo "chmod +x ~/mox-daemons/lick-run/momo-tmp/run.sh"  >> "$filepath/ninan.txt"
        
        echo "执行指令" >> "$filepath/ninan.txt"
        cd "$filepath"
        echo "workdir位于：：" >> "$filepath/ninan.txt"
        pwd >> "$filepath/ninan.txt"
        sh -c "~/mox-daemons/keep-slimy/lick-run/momo-tmp/run.sh | tee  "$filepath/水水.txt"  "
        echo "清理run.sh/文案/log" >> "$filepath/ninan.txt"
        echo "~/mox-daemons/keep-slimy/lick-run/momo-tmp/run.sh  "  >> "$filepath/ninan.txt"
        rm ~/mox-daemons/keep-slimy/lick-run/momo-tmp/run.sh >> "$filepath/ninan.txt"
        
        echo ""  >> "$filepath/ninan.txt"
        #
        #cat "$filepath/ninan.txt"
        #
        echo "以下是log" >> "$filepath/ninan.txt"
        cat "ninan.txt" "水水.txt" >  "运行结果.txt"
        
        rm "$filepath/水水.txt"
        
        rm "$filepath/ninan.txt"
        echo "以上为日志"
        echo "日志结束，执行结束时间为" && date
        echo ---------------------------------------------------------
        
    fi    
  
